package com.ymatou.iapi.trading.userquery.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.BargainGroupService;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.req.BizVo;
import com.ymatou.iapi.trading.operate.parameter.req.ChangeOrderBizStatusBean;
import com.ymatou.iapi.trading.operate.service.ChangeOrderBizStatusCall;
import com.ymatou.iapi.trading.ordersync.service.SyncVerifyService;
import com.ymatou.iapi.trading.userquery.parameter.resp.GetBuyerOrderInfoV2Resp;
import com.ymatou.iapi.trading.userquery.service.GetBuyerOrderInfoV2Call;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymatou.query.trading.api.parameter.GetBuyerOrderInfoBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.PaymentProxyCallService;
import com.ymttest.common.enums.BargainGroupStatusEnum;
import com.ymttest.common.enums.BargainGroupSubTypeEnum;
import com.ymttest.common.order.userquery.OrderInfoBuilder;
import com.ymttest.common.order.userquery.model.vo.BargainGroupInfo;
import com.ymttest.common.order.userquery.model.vo.OrderInfo;
import com.ymttest.database.model.mongo.query.Order;
import com.ymttest.database.mongo.OrderQueryWapper;
import com.ymttest.database.mongo.converter.ConvertUtils;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;

/**
 * 获取买家订单列表
 */
@TestSuite
public class Ts_GetBuyerOrderInfoV2 {

	private static GetBuyerOrderInfoBean req;
	private static GetBuyerOrderInfoV2Call getBuyerOrderInfoV2Call;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取买家订单列表");
	}

	@Before
	public void caseUp() {
		req = new GetBuyerOrderInfoBean();
		getBuyerOrderInfoV2Call = new GetBuyerOrderInfoV2Call();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getBuyerOrderInfoV2Call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void constructExpectResponse(GetBuyerOrderInfoV2Resp resp) {
		OrderQueryWapper orderQuery = new OrderQueryWapper();
		Order order = orderQuery.findById(req.getOrderId());
		ConvertUtils.convertBigDecimal(order);
		if (order != null && order.getUserId().equals(req.getUserId())) {
			resp.setOrderInfo(OrderInfoBuilder.convertFromOrder(order, true));
		}
	}

	private void verify() {
		GetBuyerOrderInfoV2Resp expectResponse = new GetBuyerOrderInfoV2Resp();
		constructExpectResponse(expectResponse);
		GetBuyerOrderInfoV2Resp actualResponse = getBuyerOrderInfoV2Call
				.getBuyerOrderInfoResponseV2();

		OrderInfo expect = expectResponse.getOrderInfo();
		OrderInfo actual = actualResponse.getOrderInfo();
		expect.verify(actual);
		if (actual != null) {
			BargainGroupInfo act = actual.getBargainGroup();
			BargainGroupInfo exp = new Ts_GetBuyerOrderInfosV2()
					.getBargainGroupInfoFromMssql((int) expect.getOrderId());
			if (exp == null) {
				DataUtil.verify(
						null,
						act,
						String.format(
								"验证 sqlserver中订单id:%d 没有参加砍价团(orderInfos.bargainGroup == null)",
								expect.getOrderId()));
			} else {
				DataUtil.verifyObject(exp, act, "砍价团信息orderInfos.bargainGroup");
			}
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_001() {
		Logger.start(true, "获取买家订单列表, 参数全传，查询成功");
		try {
			OrderCreate o = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int orderId = o.create1_M2cOrder(userId, sellerId).getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setOrderId(orderId);
			req.setUserId(userId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_002() {
		Logger.start(true, "已付款,获取买家订单列表, 参数全传，查询成功");
		try {

			OrderCreate o = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int orderId = o.create2_PayOrder(userId, sellerId).getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setOrderId(orderId);
			req.setUserId(userId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_003() {
		Logger.start(true, "订单状态为3 取消订单,根据主单号获取订单详情");
		try {
			OrderCreate o = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int orderId = o.create12_UserCancelOrder(userId, sellerId)
					.getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_004() {
		Logger.start(true, "订单状态为 接单,根据主单号获取订单详情");
		try {
			OrderCreate o = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int orderId = o.create17_AcceptOrder(userId, sellerId)
					.getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_005() {
		Logger.start(true, "订单状态为 发货,根据主单号获取订单详情");
		try {
			OrderCreate o = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int orderId = o.create3_ShippedOrder(userId, sellerId)
					.getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_006() {
		Logger.start(true, "订单状态为 确认收货根据主单号获取订单详情");
		try {

			OrderCreate o = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int orderId = o.create4_ReceivedOrder(userId, sellerId)
					.getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_007() {
		Logger.start(true, "订单状态为 申请退款根据主单号获取订单详情");
		try {
			OrderCreate o = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int orderId = o.create4_ApplyRefundOrder(userId, sellerId)
					.getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_008() {
		Logger.start(true, "订单状态为 退款成功 根据主单号获取订单详情");
		try {
			OrderCreate o = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int orderId = o.create4_RefundOrder(userId, sellerId).getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_009() {
		Logger.start(true, "买家用户id为null");
		try {

			OrderCreate o = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int orderId = o.create1_M2cOrder(userId, sellerId).getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			SyncVerifyService.getSyncResult(orderId);

			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			DataUtil.verify(400, getBuyerOrderInfoV2Call.getCode(), "验证Code");
			// Logger.verifyEquals(getBuyerOrderInfoV2Call_net.getBCode(),
			// getBuyerOrderInfoV2Call.getBCode(), "验证BCode");
			// Logger.verifyEquals(getBuyerOrderInfoV2Call_net.getMsg(),
			// getBuyerOrderInfoV2Call.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_010() {
		Logger.start(true, "订单状态为 发货,根据订单号获取订单详情");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));

			OrderCreate o = new OrderCreate();

			int orderId = o.create3_ShippedOrder(userId, sellerId)
					.getiOrderId();
			int mainOrderId = o.create3_ShippedOrder(userId, sellerId)
					.getiMainOrderId();

			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_011() {
		Logger.start(true, "验证砍价团信息，查询成功, 团长开团 组团失败");
		try {
			PlaceOrderResp placeOrderResp = BargainGroupService.openGroup(null);
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			SyncVerifyService.getSyncResult(orderId);

			BizVo bizVo = new BizVo();
			bizVo.setOrderId(orderId);
			bizVo.setBizSubType(BargainGroupSubTypeEnum.BARGAINER.getValue()); //1：开团(团长)  2：参团(团员)
			bizVo.setBizStatus(BargainGroupStatusEnum.GRP_FAIL.getValue()); //砍价团状态(-3：组团失败 -2：参团失败 -1：开团失败  0：初始状态 1：开团成功 2：参团成功 3：组团成功)
			bizVo.setBizType(1);
			bizVo.setBizId(placeOrderResp.getBizId());

			List<BizVo> bizVos = new ArrayList<BizVo>();
			bizVos.add(bizVo);

			ChangeOrderBizStatusBean changeorderbizstatusBean = new ChangeOrderBizStatusBean();
			changeorderbizstatusBean.setBizVos(bizVos);
			ChangeOrderBizStatusCall changeorderbizstatusCall = new ChangeOrderBizStatusCall();
			changeorderbizstatusCall.setData(changeorderbizstatusBean);
			changeorderbizstatusCall.callService();

			Thread.sleep(2000l);

			req.setUserId(placeOrderResp.getUserId());
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_012() {
		Logger.start(true, "使用宝付支付订单,获取买家订单列表, 参数全传，查询成功");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder5();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();

			int thirdTradingId = new OrderCallService().orderPayRequest(userId,
					BigDecimal.ZERO, tradingId, "13");

			PaymentProxyCallService.notifyCall("70",
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(), userId);

			SyncVerifyService.getSyncResult(orderId);

			req.setOrderId(orderId);
			req.setUserId(userId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_013() {
		Logger.start(true, "虚拟商品下单, 参数全传，查询成功");
		try {

			PlaceOrderResp placeOrderResp = BargainGroupService
					.placeVirtualProduct();

			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);

			//new OrderCallService().acceptOrder(orderId, sellerId);

			SyncVerifyService.getSyncResult(orderId);

			req.setOrderId(orderId);
			req.setUserId(userId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}